import React from 'react';
import {parse, stringify} from 'qs';

export function getNameByFileDeal(fileState) {
  let text = "--";
  if (fileState == 0) {
    text = "处理中";
  } else if (fileState == 1) {
    text = "处理成功";
  } else if (fileState == 2) {
    text = "处理失败";
  }
  return text;
}

export function getNameByApplyType(type) {
  let text = "--";
  switch (type) {
    case 0:
      text = "共享";
      break;
    case 1:
      text = "流转";
      break;
    case 2:
      text = "外发";
      break;
    case 3:
      text = "下载";
      break;
    case 4:
      text = "编辑";
      break;
    default :
      break;
  }
  return text;
}

/***
 * 将b 转成KB,MB,GB
 * @param type
 * @returns {string}
 */
export function getReadableSize(limit) {
  var size = "";
  if (limit < 0.1 * 1024) {                            //小于0.1KB，则转化成B
    size = limit.toFixed(2) + "B"
  } else if (limit < 0.1 * 1024 * 1024) {            //小于0.1MB，则转化成KB 123
    size = (limit / 1024).toFixed(2) + "KB"
  } else if (limit < 0.1 * 1024 * 1024 * 1024) {        //小于0.1GB，则转化成MB
    size = (limit / (1024 * 1024)).toFixed(2) + "MB"
  } else {                                            //其他转化成GB
    size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB"
  }

  var sizeStr = size + "";                        //转成字符串
  var index = sizeStr.indexOf(".");                    //获取小数点处的索引
  var dou = sizeStr.substr(index + 1, 2)            //获取小数点后两位的值
  if (dou == "00") {                                //判断后两位是否为00，如果是则删除00
    return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
  }
  return size;

}

/***
 * 检查是否安装了在线编辑客户端
 */
export function hasInstalledEditApp() {
  var result = false;
  var url = 'http://127.0.0.1:39835/BwdaOnlineEditServer';
  // if (document.location.protocol == 'https:') {
  //   url = 'https://127.0.0.1:39855/BwdaOnlineEditServer';
  // }
  ;
  var request = new XMLHttpRequest();
  request.open('POST', url, false);
  request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  request.onreadystatechange = function () {
    if (request.readyState == 4) {
      if (request.status == 200) {
        var obj = eval("(" + request.responseText + ")");
        if (obj.error) {
          console.log('fail' + obj.return_val);
        } else {
          result = true;
          console.log('success' + obj.return_val);
        }
        ;
      } else {
        console.log(request.status);
      }
      ;
    }
    ;
  };
  // json=json.replace(/\\/ig,"\\\\");
  let json = '{"function":"version"}';
  eval("json = '" + json + "';");
  try {
    request.send('json=' + json);
  }
  catch (e) {
  }
  return result;
}
